//
//  DZRedomAlertView.m

#import "DZRedomAlertView.h"
#import "zhPopupController.h"
#import "AppDelegate.h"
@interface DZRedomAlertView()
@property (nonatomic,strong) zhPopupController *redomAlertViewPopUp;
@property (nonatomic, copy) void (^RedomAlertBlock)(NSString *redomNum,NSString *redomType,BOOL isRepert);
@property (nonatomic, copy)NSString * redomNum;
@property (nonatomic, copy)NSString * redomType;
@property (nonatomic, assign)BOOL isRepert;

@property (nonatomic, strong)UILabel * redomNumLab;
@property (nonatomic, strong)UIButton * redomNumBtn;
@property (nonatomic, strong)UIButton * englishNumBtn;
@property (nonatomic, strong)UIButton * allNumBtn;
@property (nonatomic, strong)UIButton * selectNumBtn;

@property (nonatomic, strong)UIButton * repeatBtn;
@end
@implementation DZRedomAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = DZRatio(20);
        [self makeSubView];
    }
    return self;
}
- (void)makeSubView{
    UILabel *redomHeadLab = [[UILabel alloc] init];
    redomHeadLab.text = @"随机数设置";
    redomHeadLab.textColor = DZHex(@"#333333");
    redomHeadLab.font = [UIFont systemFontOfSize:DZRatio(36) weight:UIFontWeightRegular];
    [self addSubview:redomHeadLab];
    [redomHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DZRatio(47)));
        make.centerX.equalTo(self);
        make.height.equalTo(@(DZRatio(43)));
    }];
    UILabel *redomNumHeadLab = [[UILabel alloc] init];
    redomNumHeadLab.text = @"随机数(位)";
    redomNumHeadLab.textColor = DZHex(@"#333333");
    redomNumHeadLab.font = [UIFont systemFontOfSize:DZRatio(30) weight:UIFontWeightRegular];
    [self addSubview:redomNumHeadLab];
    [redomNumHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DZRatio(156)));
        make.left.equalTo(@(DZRatio(40)));
        make.height.equalTo(@(DZRatio(36)));
    }];
    UIButton * redomNumAddBtn = [[UIButton alloc] init];
    [redomNumAddBtn setBackgroundImage:[UIImage imageNamed:@"dzRedomNumAdd"] forState:UIControlStateNormal];
    [redomNumAddBtn addTarget:self action:@selector(redomNumAdd) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:redomNumAddBtn];
    [redomNumAddBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DZRatio(47)));
        make.top.equalTo(@(DZRatio(153)));
        make.width.equalTo(@(DZRatio(44)));
        make.height.equalTo(@(DZRatio(44)));
    }];
    self.redomNumLab = [[UILabel alloc] init];
    self.redomNumLab.textColor = DZHex(@"#2D353B");
    self.redomNumLab.font = [UIFont systemFontOfSize:DZRatio(40) weight:UIFontWeightRegular];
    self.redomNumLab.textAlignment = NSTextAlignmentCenter;
    self.redomNumLab.backgroundColor = DZHexA(@"#B5D4EE",0.4);
    self.redomNumLab.layer.cornerRadius = DZRatio(10);
    self.redomNumLab.clipsToBounds = YES;
    [self addSubview:self.redomNumLab];
    [self.redomNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DZRatio(140)));
        make.right.equalTo(@(-DZRatio(127)));
        make.width.equalTo(@(DZRatio(214)));
        make.height.equalTo(@(DZRatio(70)));
    }];
    UIButton * redomNumCutBtn = [[UIButton alloc] init];
    [redomNumCutBtn setBackgroundImage:[UIImage imageNamed:@"dzRedomNumCut"] forState:UIControlStateNormal];
    [redomNumCutBtn addTarget:self action:@selector(redomNumCut) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:redomNumCutBtn];
    [redomNumCutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DZRatio(370)));
        make.top.equalTo(@(DZRatio(153)));
        make.width.equalTo(@(DZRatio(44)));
        make.height.equalTo(@(DZRatio(44)));
    }];
    UIView *redomNumTypeLineView = [[UIView alloc] init];
    redomNumTypeLineView.backgroundColor = DZHex(@"#E5E5E5");
    [self addSubview:redomNumTypeLineView];
    [redomNumTypeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(41)));
        make.right.equalTo(@(-DZRatio(17)));
        make.top.equalTo(@(DZRatio(236)));
        make.height.equalTo(@(DZRatio(1)));
    }];
    
    UILabel *redomTypeHeadLab = [[UILabel alloc] init];
    redomTypeHeadLab.text = @"文字";
    redomTypeHeadLab.textColor = DZHex(@"#333333");
    redomTypeHeadLab.font = [UIFont systemFontOfSize:DZRatio(30) weight:UIFontWeightRegular];
    [self addSubview:redomTypeHeadLab];
    [redomTypeHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DZRatio(278)));
        make.left.equalTo(@(DZRatio(40)));
        make.height.equalTo(@(DZRatio(36)));
    }];
    self.allNumBtn = [[UIButton alloc] init];
    self.allNumBtn.tag = 3;
    [self.allNumBtn setTitle:@"英文和数字" forState:UIControlStateNormal];
    [self.allNumBtn setTitleColor:DZHex(@"#666666") forState:UIControlStateNormal];
    [self.allNumBtn setTitleColor:DZHex(@"#4594D5") forState:UIControlStateSelected];
    self.allNumBtn.titleLabel.font = [UIFont systemFontOfSize:DZRatio(28) weight:UIFontWeightRegular];
    self.allNumBtn.layer.borderColor = DZHex(@"#999999").CGColor;
    self.allNumBtn.layer.borderWidth = DZRatio(2);
    self.allNumBtn.layer.cornerRadius = DZRatio(4);
    self.allNumBtn.clipsToBounds = YES;
    [self.allNumBtn addTarget:self action:@selector(allNum) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.allNumBtn];
    [self.allNumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DZRatio(47)));
        make.top.equalTo(@(DZRatio(266)));
        make.width.equalTo(@(DZRatio(175)));
        make.height.equalTo(@(DZRatio(60)));
    }];
    self.englishNumBtn = [[UIButton alloc] init];
    self.englishNumBtn.tag = 2;
    [self.englishNumBtn setTitle:@"英文" forState:UIControlStateNormal];
    [self.englishNumBtn setTitleColor:DZHex(@"#666666") forState:UIControlStateNormal];
    [self.englishNumBtn setTitleColor:DZHex(@"#4594D5") forState:UIControlStateSelected];
    self.englishNumBtn.titleLabel.font = [UIFont systemFontOfSize:DZRatio(28) weight:UIFontWeightRegular];
    self.englishNumBtn.layer.borderColor = DZHex(@"#999999").CGColor;
    self.englishNumBtn.layer.borderWidth = DZRatio(2);
    self.englishNumBtn.layer.cornerRadius = DZRatio(4);
    self.englishNumBtn.clipsToBounds = YES;
    [self.englishNumBtn addTarget:self action:@selector(englishNum) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.englishNumBtn];
    [self.englishNumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.allNumBtn.mas_left).offset(-DZRatio(24));
        make.top.equalTo(@(DZRatio(266)));
        make.width.equalTo(@(DZRatio(133)));
        make.height.equalTo(@(DZRatio(60)));
    }];
    self.redomNumBtn = [[UIButton alloc] init];
    self.redomNumBtn.tag = 1;
    [self.redomNumBtn setTitle:@"数字" forState:UIControlStateNormal];
    [self.redomNumBtn setTitleColor:DZHex(@"#666666") forState:UIControlStateNormal];
    [self.redomNumBtn setTitleColor:DZHex(@"#4594D5") forState:UIControlStateSelected];
    self.redomNumBtn.titleLabel.font = [UIFont systemFontOfSize:DZRatio(28) weight:UIFontWeightRegular];
    self.redomNumBtn.layer.borderColor = DZHex(@"#999999").CGColor;
    self.redomNumBtn.layer.borderWidth = DZRatio(2);
    self.redomNumBtn.layer.cornerRadius = DZRatio(4);
    self.redomNumBtn.clipsToBounds = YES;
    [self.redomNumBtn addTarget:self action:@selector(redomNorNum) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.redomNumBtn];
    [self.redomNumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.englishNumBtn.mas_left).offset(-DZRatio(24));
        make.top.equalTo(@(DZRatio(266)));
        make.width.equalTo(@(DZRatio(133)));
        make.height.equalTo(@(DZRatio(60)));
    }];
    UIView *redomNumLineView = [[UIView alloc] init];
    redomNumLineView.backgroundColor = DZHex(@"#E5E5E5");
    [self addSubview:redomNumLineView];
    [redomNumLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(41)));
        make.right.equalTo(@(-DZRatio(17)));
        make.top.equalTo(@(DZRatio(356)));
        make.height.equalTo(@(DZRatio(1)));
    }];
    UILabel *redomRepratHeadLab = [[UILabel alloc] init];
    redomRepratHeadLab.text = @"重复";
    redomRepratHeadLab.textColor = DZHex(@"#333333");
    redomRepratHeadLab.font = [UIFont systemFontOfSize:DZRatio(30) weight:UIFontWeightRegular];
    [self addSubview:redomRepratHeadLab];
    [redomRepratHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DZRatio(399)));
        make.left.equalTo(@(DZRatio(40)));
        make.height.equalTo(@(DZRatio(36)));
    }];
    self.repeatBtn = [[UIButton alloc] init];
    [self.repeatBtn setBackgroundImage:DZImageNamed(@"dz_ReportClose") forState:UIControlStateNormal];
    [self.repeatBtn setBackgroundImage:DZImageNamed(@"dz_ReportOpen") forState:UIControlStateSelected];
    [self.repeatBtn addTarget:self action:@selector(repeat) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.repeatBtn];
    [self.repeatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DZRatio(47)));
        make.top.equalTo(@(DZRatio(389)));
        make.width.equalTo(@(DZRatio(92)));
        make.height.equalTo(@(DZRatio(56)));
    }];
    UIButton * redomNumCancleBtn = [[UIButton alloc] init];
    [redomNumCancleBtn setBackgroundImage:[UIImage imageNamed:@"dzRedomNumCancle"] forState:UIControlStateNormal];
    [redomNumCancleBtn addTarget:self action:@selector(redomNumCancle) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:redomNumCancleBtn];
    [redomNumCancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(73)));
        make.top.equalTo(@(DZRatio(517)));
        make.width.equalTo(@(DZRatio(262)));
        make.height.equalTo(@(DZRatio(80)));
    }];
    UIButton * redomNumSureBtn = [[UIButton alloc] init];
    [redomNumSureBtn setBackgroundImage:[UIImage imageNamed:@"dzRedomNumSure"] forState:UIControlStateNormal];
    [redomNumSureBtn addTarget:self action:@selector(redomNumSure) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:redomNumSureBtn];
    [redomNumSureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DZRatio(73)));
        make.top.equalTo(@(DZRatio(517)));
        make.width.equalTo(@(DZRatio(262)));
        make.height.equalTo(@(DZRatio(80)));
    }];
}
- (void)redomNumAdd{
    _redomNum = [NSString stringWithFormat:@"%d",[_redomNum intValue]+1];
    self.redomNumLab.text = _redomNum;
}
- (void)redomNumCut{
    if([_redomNum intValue] > 1){
        _redomNum = [NSString stringWithFormat:@"%d",[_redomNum intValue]-1];
    }
    self.redomNumLab.text = _redomNum;
}
- (void)allNum{
    self.allNumBtn.selected = YES;
    self.selectNumBtn.selected = NO;
    self.selectNumBtn = self.allNumBtn;
}
- (void)englishNum{
    self.englishNumBtn.selected = YES;
    self.selectNumBtn.selected = NO;
    self.selectNumBtn = self.englishNumBtn;
}
- (void)redomNorNum{
    self.redomNumBtn.selected = YES;
    self.selectNumBtn.selected = NO;
    self.selectNumBtn = self.redomNumBtn;
}
- (void)repeat{
    self.repeatBtn.selected = !self.repeatBtn.isSelected;
}
- (void)redomNumCancle{
    [_redomAlertViewPopUp dismiss];
}
- (void)redomNumSure{
    if(self.RedomAlertBlock){
        NSString *redomType = [NSString stringWithFormat:@"%zd",self.selectNumBtn.tag];
        BOOL isRepert = self.repeatBtn.isSelected;
        self.RedomAlertBlock(_redomNum, redomType, isRepert);
        [_redomAlertViewPopUp dismiss];
    }
}
-(void)setRedomNum:(NSString *)redomNum{
    _redomNum = redomNum;
    self.redomNumLab.text = _redomNum;
}
-(void)setRedomType:(NSString *)redomType{
    _redomType = redomType;
    if([_redomType isEqualToString:@"1"]){
        [self redomNorNum];
    }else if([_redomType isEqualToString:@"2"]){
        [self englishNum];
    }else if([_redomType isEqualToString:@"3"]){
        [self allNum];
    }
}
-(void)setIsRepert:(BOOL)isRepert{
    _isRepert = isRepert;
    self.repeatBtn.selected = _isRepert;
}
+ (void)showRedomAlertView:(NSString *)redomNum redomType:(NSString *)redomType isRepert:(BOOL)isRepert complaint:(void(^)(NSString *redomNum,NSString *redomType,BOOL isRepert))complaint{
    DZRedomAlertView *redomAlertView = [[DZRedomAlertView alloc] init];
    zhPopupController *redomAlertViewPopUp = [[zhPopupController alloc] initWithView:redomAlertView size:CGSizeMake(DZRatio(694), DZRatio(639))];
    redomAlertView.redomNum = redomNum;
    redomAlertView.redomType = redomType;
    redomAlertView.isRepert = isRepert;

    redomAlertView.RedomAlertBlock = complaint;
    redomAlertView.redomAlertViewPopUp = redomAlertViewPopUp;
    redomAlertViewPopUp.presentationStyle = zhPopupSlideStyleTransform;
    redomAlertViewPopUp.layoutType = zhPopupLayoutTypeCenter;
    redomAlertViewPopUp.presentationTransformScale = 0.85;
    redomAlertViewPopUp.dismissonTransformScale = 0.85;
    redomAlertViewPopUp.maskAlpha = 0.6;
    [redomAlertViewPopUp showInView:((AppDelegate*)([UIApplication sharedApplication].delegate)).window completion:^{
      
    }];
}
@end
